package com.meihuak.cloud_ordering.controller;

import com.meihuak.cloud_ordering.bean.Manager;
import com.meihuak.cloud_ordering.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author 韩迪
 * @date 2022/04/05 14:39
 **/
@Slf4j
@Controller
@RequestMapping("/sys")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam (value = "name") String name,
                        @RequestParam (value = "userkey")String key,
                        HttpSession session      ){
        System.out.println(name+"__"+key);
        Manager manager=managerService.login(name,key);
        ModelAndView m = new ModelAndView();
        if (manager!=null){//成功登陆
            System.out.println("查出结果");
            session.setAttribute("id",manager.getId());
            m.setViewName("manager/manager");
            m.addObject("info",manager);
            return m;
        }
        System.out.println("未查出");
        m.setViewName("manager/error");
        return m;
    }

    @RequestMapping("/info")
    public ModelAndView getmaninfo( HttpSession session){
        ModelAndView m = new ModelAndView();
        Integer id = (int)session.getAttribute("id");
        Manager manager=managerService.getById(id);
        m.setViewName("manager/manager1");
        m.addObject("info",manager);
        return m;
    }

    @RequestMapping("/lg")
    public String loginn(Model m){
        return "manager/login";
    }


    @RequestMapping("/manpage")
    public ModelAndView getManagerInfo(HttpSession session){
        Integer id = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        Manager manager=managerService.getById(id);
        if (manager!=null){

            m.setViewName("manager/manager");
            return m;
        }
        m.setViewName("manager/error");

        return m;
    }



}
